package com.zara.store.client.clientmodel;

import java.math.BigDecimal;

import com.zara.store.client.model.StoreProxy;

import framework.clientmodel.Model;

public class RetailModel extends Model {
	
	private RetailBasket basketModel;
	private RetailInvoice invoiceModel;
	private RetailProduct productModel;
	
	public RetailModel(StoreProxy sp) {
		basketModel = new RetailBasket();
		invoiceModel = new RetailInvoice(sp);
		productModel = new RetailProduct(sp);
	}
	
	public void removeLines(int[] rowIndexes) {
		if(basketModel.size()>0) {
			for(int i = rowIndexes.length-1; i>=0; i--) {
				basketModel.removeLine(rowIndexes[i]);
				notifyViews("basket");
			}
		}
		clearInvoice();
	}
	public void removeLines() {
		if(basketModel.size()>0) {
			for(int i = basketModel.size()-1; i>=0; i--) {
				basketModel.removeLine(i);
				notifyViews("basket");
			}
		}
		clearInvoice();
	}
	public void setLineAmount(BigDecimal amount) {
		basketModel.setLineAmount(amount);
		clearInvoice();
		notifyViews("basket");
	}

	private void clearInvoice() {
		if(invoiceModel.getNumber() > 0 ) {
			invoiceModel.clearNumber();
			notifyViews("invoice");
		}
	}
	public void setLineOffer(boolean offer) {
		basketModel.setLineOffer(offer);
		clearInvoice();
		notifyViews("basket");
	}
	public void addProduct() {
		if(productModel.getProductVO().getStock().compareTo(BigDecimal.ZERO) > 0) {
			basketModel.addProduct(productModel);
			clearInvoice();
			notifyViews("basket");
		}
	}
	public RetailBasket getBasketModel() {
		return basketModel;
	}

	public void printInvoice() {
		notifyViews("invoice");
	}

	public RetailInvoice getInvoiceModel() {
		return invoiceModel;
	}

	public void queryProduct(String barcode) {
		productModel.queryProduct(barcode);
		notifyViews("product");
	}

	public RetailProduct getProductModel() {
		return productModel;
	}

	public void createInvoice() {
		if(basketModel.size()>0) { //si tengo productos en el basket genero un invoice
			if(invoiceModel.getNumber()==0) {
				invoiceModel.setBasket(basketModel);
				invoiceModel.createInvoice();
			}
			if(invoiceModel.getNumber() > 0) {
				printInvoice();
			} else {
				notifyViews("basket");
			}
		}
	}

}
